package com.xapp.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Telephony;
import com.xapp.yhEntities.MessageInfoEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author YH_CK
 * data 2018.09.14 14:30
 * describe 短信获取工具类
 **/
public class MessageInfoUtils {
    private static final String TAG = MessageInfoUtils.class.getSimpleName();
    private static MessageInfoUtils instance = null;
    private static Context mContext;
    private static final String[] TELEPHONY_ION = new String[]{
            Telephony.Sms.ADDRESS,
            Telephony.Sms.BODY,
            Telephony.Sms.DATE,
            Telephony.Sms.TYPE
    };

    public static MessageInfoUtils getInstance(Context context) {
        if (instance == null) {
            instance = new MessageInfoUtils();
        }
        mContext = context;
        return instance;
    }


    public String getSmsInfo() {
        List<MessageInfoEntity> mSmsInfoList = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        ContentResolver resolver = mContext.getContentResolver();
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(currentDate);//把当前时间赋给日历
        calendar.add(Calendar.MONTH, -3);  //设置为前3月
        //calendar.add(calendar.DAY_OF_MONTH, -24);
        currentDate = calendar.getTime();//获取3个月前的时间
        Uri uri = Uri.parse("content://sms");
        Cursor cursor = resolver.query(uri, TELEPHONY_ION, Telephony.Sms.DATE + ">=" + currentDate.getTime(),
                null, null);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                MessageInfoEntity entity = new MessageInfoEntity();
                entity.setTelphone(cursor.getString(cursor.getColumnIndex("address")));
                entity.setBody(cursor.getString(cursor.getColumnIndex("body")));
                Date mDate = new Date(cursor.getLong(cursor.getColumnIndex("date")));
                String msgDate = simpleDateFormat.format(mDate);
                entity.setDate(msgDate);
                entity.setType(String.valueOf(cursor.getInt(cursor.getColumnIndex("type"))));
                mSmsInfoList.add(entity);
            }
        } else {
            try {
                jsonObject.put("status", "3001");
                jsonObject.put("data", "");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jsonObject.toString();
        }

        try {
            if (mSmsInfoList.size() <= 0) {
                jsonObject.put("status", "1000");
                jsonObject.put("data", "");
            } else {
                for (MessageInfoEntity entity : mSmsInfoList) {
                    JSONObject tmpObj = new JSONObject();
                    tmpObj.put("phone", entity.getTelphone());
                    tmpObj.put("content", entity.getBody());
                    tmpObj.put("contactTime", entity.getDate());
                    tmpObj.put("type", entity.getType());
                    jsonArray.put(tmpObj);
                }
                jsonObject.put("data", jsonArray);
                jsonObject.put("status", "1000");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return jsonObject.toString();
    }

}
